package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

/**
 * A simple demonstration application showing how to create a bar chart.
 *
 */
@SuppressWarnings("serial")
public class BarChart extends JPanel {

	String rowTitle;
	String columnTitle;
	Set<Float> rowValues;
	Collection<String> columnValues;
	final ChartPanel chartPanel;
	
    public BarChart(String rowT, String columnT, Set<Float> rowV, Collection<String> columnV) {

    	rowTitle = rowT;
    	columnTitle = columnT;
    	rowValues = rowV;
    	columnValues = columnV;
    	
    	setPreferredSize(new Dimension(ViewConstants.CHART_WIDTH, ViewConstants.CHART_HEIGHT - 100));
		setMinimumSize(new Dimension(ViewConstants.CHART_WIDTH, ViewConstants.CHART_HEIGHT - 100));
		setLayout(new GridBagLayout());
		setBackground(new Color(16777215));
		
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.fill = GridBagConstraints.HORIZONTAL;

		final CategoryDataset dataset = createDataset();
        final JFreeChart chart = createChart(dataset);
        chartPanel = new ChartPanel(chart);
        
        chartPanel.setPreferredSize(new Dimension(ViewConstants.CHART_WIDTH, ViewConstants.CHART_HEIGHT - 100));
        chartPanel.setMinimumSize(new Dimension(ViewConstants.CHART_WIDTH, ViewConstants.CHART_HEIGHT - 100));
        
        constraints.gridx = 0;
		constraints.gridy = 0;
        add(chartPanel,constraints);
    }

    /**
     * Returns a sample dataset.
     * @return The dataset.
     */
    private CategoryDataset createDataset() {
        
      	final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    	Iterator<Float> i = rowValues.iterator();
    	Iterator<String> j = columnValues.iterator();
    	String currentString = "";
    	float currentFloat = 0;
    	
    	while(i.hasNext()) {
    		currentFloat = i.next();
    		currentString = j.next();
    		dataset.addValue(currentFloat, rowTitle, currentString);
    	}
 
        return dataset;        
    }
    

    private CategoryDataset createDataset(Set<Float> rowV, Collection<String> columnV) {
        
    	final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    	Iterator<Float> i = rowV.iterator();
    	Iterator<String> j = columnV.iterator();
    	String currentString = "";
    	float currentFloat = 0;
    	
    	while(i.hasNext()) {
    		currentFloat = i.next();
    		currentString = j.next();
    		dataset.addValue(currentFloat, rowTitle, currentString);
    	}
    	
        return dataset;        
    }
    
    /**
     * Creates a sample chart.
     * @param dataset  the dataset.
     * @return The chart.
     */
    private JFreeChart createChart(final CategoryDataset dataset) {

        final JFreeChart chart = ChartFactory.createBarChart(
            "",      				// chart title
            columnTitle,          	    // domain axis label
            rowTitle,                	// range axis label
            dataset,                  	// data
            PlotOrientation.VERTICAL,	// orientation
            false,                    	// include legend
            true,                     	// tooltips?
            false                     	// URLs?
        );

        // set the background color for the chart...
        chart.setBackgroundPaint(Color.white);

        // get a reference to the plot for further customisation...
        final CategoryPlot plot = chart.getCategoryPlot();
        plot.setBackgroundPaint(Color.white);
        plot.setDomainGridlinePaint(Color.black);
        plot.setRangeGridlinePaint(Color.black);

        // set the range axis to display integers only...
        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

        // disable bar outlines...
        final BarRenderer renderer = (BarRenderer) plot.getRenderer();
        renderer.setDrawBarOutline(false);
        
        // set up gradient paints for series...
        final GradientPaint gp0 = new GradientPaint(
            0.0f, 0.0f, Color.blue, 
            0.0f, 0.0f, Color.lightGray
        );
        renderer.setSeriesPaint(0, gp0);

        final CategoryAxis domainAxis = plot.getDomainAxis();
        domainAxis.setCategoryLabelPositions(
            CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0)
        );
        
        return chart;
        
    }
    
    public void update (Set<Float> rowV, Collection<String> columnV) {
		final CategoryDataset dataset = createDataset(rowV, columnV);
    	final JFreeChart chart = createChart(dataset);

    	chartPanel.setChart(chart);
    }

}